#!/bin/bash

#===============纯函数库=====================
#LANG="en_US.UTF-8"
#如果脚本或者输出是乱码，使用LANG设置一下：LANG="en_US.UTF-8"


function test_lib(){
	echo "test_lib ok，函数库导入成功";
}


#检查目录是否存在
function func_check_dir() {
	#mkdir
	dir_=$1
	if  [ -d "$dir_" ]; then
		return 1
	else
        return 0
	fi
}


#检查目录是否存在，不存在就建立
function func_if_mkdir() {
	#mkdir
	dir_=$1
	if  [ -d "$dir_" ]; then
		echo "dir exist="$dir_
	else
		echo "dir not exist="$dir_
		mkdir -p $dir_
	fi
}



#=================================================
#================= functions =====================
#=================================================


#the first step, install openresty dir and gg-tool
function func_install_dir(){
    echo "call $0"

	if [ -f "hostguard_setup_config.dat" ]; then
		mv /hostguard_setup_config.dat /_hostguard_setup_config.dat
	fi

    if  [ -d "/data/" ]; then
        echo "check /data/ ok"
    else
        echo "error: no /data/ dir"
        exit
    fi

    if  [ -d "/data/samba/haima/" ]; then
        echo "/data/samba/haima/ existed"
    else
        echo "create /data/samba/haima/"
        mkdir /data/samba/haima/lua/modules/env/ -p

        mkdir /data/samba/haima/conf
        chmod 777 /data/samba/haima/*
        chown nobody:nobody /data/samba/haima/* -R

        echo "create /data/samba/haima/ ok"
    fi


    if  [ -f "/usr/bin/gg" ]; then
        echo "/usr/bin/gg existed"
    else
        chmod 777 /home/gg
        ln -s /home/gg  /usr/bin/gg
        echo "/usr/bin/gg set ok"
    fi


	if  [ -d "/data/logs/miniw/" ]; then
		echo "/data/logs/miniw/ created before ok"
	else
		mkdir /data/logs/miniw/ -p
		chmod 777 /data/logs/
		chmod 777 /data/logs/miniw/
	fi

	if  [ -d "/data/core_files/" ]; then
		echo "/data/core_files/ created before ok"
	else
		mkdir /data/core_files/
		chmod 777 /data/core_files/
	fi


	if  [ -d "/data/dockers/" ]; then
		echo "/data/dockers/ created before ok"
	else
		mkdir /data/dockers/
		chmod 777 /data/dockers/
	fi

}


#install required linux standard tools exe
function func_install_software() {
    echo "call $0"

	if  [ -f "/usr/bin/cpulimit" ]; then
		echo "installed before"
		return 1
	fi

	#yum update -y
	#时间不同步的时候使用(海外环境)
	#ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

	yum install lrzsz wget  -y
	yum install net-tools  -y
	yum install sysstat  -y
    yum install gcc gcc-c++ gmake  -y
    yum install pcre-devel openssl openssl-devel  -y
    yum install ImageMagick  -y
    yum install autoconf  -y
    yum install zip unzip  -y
	yum install rsync  -y
    yum install cpulimit  -y
}



#try set crontab for centos system reporter
function func_install_crontab() {
	crontab_ret=`crontab -l | grep miniw_132_centos.sh`
	if [ ${#crontab_ret} -gt 0 ]; then
		echo "crontab miniw_132_centos.sh has set"
	else
		echo "try set crontab miniw_132_centos.sh"
		echo '' >> /var/spool/cron/root
		echo '*/5 * * * *   /home/miniw_132_centos.sh > /dev/null 2>&1 &' >> /var/spool/cron/root
		echo '' >> /var/spool/cron/root

		echo ''
		echo '==== crontab set ok, plz change [name] for miniw_132_centos.sh'
		echo ''
		echo '==== using:          vi /home/miniw_132_centos.sh'
		echo ''
	fi
}



#132_rent_server_rentRsyncRoot_r/home/
function func_rsync_132_or_home() {
	func_install_rsync_client_907

	#rsync code
	echo "----home/"
	/usr/bin/rsync  --progress --port=8873 -ptv --exclude="install_std_server_*.*" \
		samba@120.24.64.132::132_haima_code_miniw_install_home_r/*.* \
		/home/ \
		--password-file /tmp/rsync_132.secrets_client_r_907


	echo "----install_files"
	if  [ -f "/home/miniw_132_centos.sh" ]; then
        echo "miniw_132_centos.sh exist ignore"
		/usr/bin/rsync  --progress --port=8873 -ptv --exclude="miniw_132_centos.sh" \
			samba@120.24.64.132::132_haima_code_miniw_install_home_r/install_files/* \
			/home/ \
			--password-file /tmp/rsync_132.secrets_client_r_907
    else
		/usr/bin/rsync  --progress --port=8873 -ptv \
			samba@120.24.64.132::132_haima_code_miniw_install_home_r/install_files/* \
			/home/ \
			--password-file /tmp/rsync_132.secrets_client_r_907
	fi


	chmod 700 /home/install_std_*.sh
	chmod 700 /home/miniw_132_centos.sh
}



#=========install openresty============
function func_install_openresty() {
    #func_install_dir
    #func_install_software

    if  [ -f "/usr/local/openresty/nginx/sbin/nginx" ]; then
        echo "nginx or installed before"
        return 1
    fi

    cd /home/

    if  [ -d "./openresty-1.13.6.2/" ]; then
        echo "./openresty-1.13.6.2/ unzip ok"
    else
        tar xzvf ./openresty-1.13.6.2.tar.gz
		cd /home/openresty-1.13.6.2/
		./configure; make;
    fi

    cd /home/openresty-1.13.6.2/
    make install;

    if  [ -f "/usr/local/openresty/nginx/sbin/nginx" ]; then
        echo "nginx or installed ok"
    else
        echo "nginx or installed fail=========="
		echo "nginx or 安装失败================"
		exit
    fi

    cd /home/
}



function func_install_ssdb(){

    if  [ -f "/usr/local/ssdb/ssdb-server" ]; then
        echo "nginx or installed before"
        return 1
    fi

    cd /home/

    if  [ -d "./ssdb-master/" ]; then
        echo "./ssdb-master/ unzip ok"
    else
        unzip ./ssdb-master_195.zip
    fi

    cd /home/ssdb-master/
    make; make install;

    if  [ -f "/usr/local/ssdb/ssdb-server" ]; then
        echo "ssdb installed ok"
    else
        echo "ssdb installed fail=========="
		echo "ssdb 安装失败================"
		exit
    fi

    cd /home/
}



#install openresty dir links:  log lua conf ( docker-in )
function func_or_ln(){

	if  [ -d "/usr/local/openresty/nginx/" ]; then
		echo "/usr/local/openresty/nginx/ exist ok"
	else
		echo "openresty not installed. exit."
		exit
	fi

	if  [ -d "/data/logs/miniw/" ]; then
		echo "/data/logs/miniw/ created before ok"
	else
		mkdir /data/logs/miniw/ -p
		chmod 777 /data/logs/
		chmod 777 /data/logs/miniw/
	fi

	cd /usr/local/openresty/nginx/

	if  [ -d "./bak" ]; then
		echo "openresty/bak created before ok"
	else
		mkdir ./bak
		mv ./conf ./bak/
		mv ./logs ./bak/

		ln -s /data/logs/
		ln -s /data/samba/haima/conf/
		ln -s /data/samba/haima/lua/

		cp -f ./bak/conf/* ./conf/

		if  [ -f "/usr/local/openresty/nginx/html/favicon.ico" ]; then
			echo "favicon.ico set before ok"
		else
			> /usr/local/openresty/nginx/html/index.html
			> /usr/local/openresty/nginx/html/50x.html
			> /usr/local/openresty/nginx/html/404.html
			wget 'http://120.24.64.132:8087/favicon.ico'
			mv -f favicon.ico /usr/local/openresty/nginx/html/
		fi
	fi

}




#======================keeper install=================



function func_rsync_miniw_activity_lua_modules(){
	func_install_rsync_client_907

	#rsync code
	/usr/bin/rsync  --progress --port=8873 -ptvr --include="*/" --include="*.lua" --exclude="*" \
		samba@164.52.98.28::28_haima_code_miniw_activity_r/lua/modules/$1/ \
		/usr/local/openresty/nginx/lua/modules/$1/ \
		--password-file /tmp/rsync_132.secrets_client_r_907
}


function func_rsync_or_server_config(){

	if   [ "$1" == "openresty_keeper_x.conf" ]; then
		conf_dir=keeper_server/$1
		echo "loading $1"
	elif [ "$1" == "openresty_dc_x.conf" ]; then
		conf_dir=data_cache/$1
		echo "loading $1"
	elif [ "$1" == "openresty_ma_gate_x.conf" ]; then
		conf_dir=ma_gate/$1
		echo "loading $1"
	elif [ "$1" == "openresty_dc_gate_x.conf" ]; then
		conf_dir=dc_gate/$1
		echo "loading $1"
	else
		echo "error_config: $1"
		exit
	fi


	func_install_rsync_client_123

	#rsync config
	/usr/bin/rsync  --progress --port=8873 -avrcz \
            samba@164.52.98.28::28_haima/openresty_configs/${conf_dir} \
            /usr/local/openresty/nginx/conf/ \
            --password-file /tmp/rsync_132.secrets_client_123

}



function func_rsync_rent_node_lua_server(){
	func_rsync_miniw_activity_lua_modules commhm
	func_rsync_miniw_activity_lua_modules html_page
	func_rsync_miniw_activity_lua_modules keeper_server
	func_rsync_miniw_activity_lua_modules rent_server
}



function func_install_keeper_server(){
	#rsync code
	func_rsync_miniw_activity_lua_modules commhm
	func_rsync_miniw_activity_lua_modules html_page
	func_rsync_miniw_activity_lua_modules center_server
	func_rsync_miniw_activity_lua_modules keeper_server

	#config
	func_rsync_or_server_config  openresty_keeper_x.conf

	echo "cd /usr/local/openresty/nginx/"
	echo "sbin/nginx -c conf/openresty_keeper_x.conf"

	func_if_mkdir "/usr/local/openresty/nginx/pids/"
	cd /usr/local/openresty/nginx/
	sbin/nginx -c conf/openresty_keeper_x.conf
}



function func_install_dc_server(){
	#rsync code
	func_rsync_miniw_activity_lua_modules commhm
	func_rsync_miniw_activity_lua_modules data_cache

	#config
	func_rsync_or_server_config  openresty_dc_x.conf

	echo "cd /usr/local/openresty/nginx/"
	echo "sbin/nginx -c conf/openresty_dc_x.conf"

	func_if_mkdir "/usr/local/openresty/nginx/pids/"
	cd /usr/local/openresty/nginx/
	sbin/nginx -c conf/openresty_dc_x.conf
}


function func_install_ma_gate_server(){
	#rsync code
	func_rsync_miniw_activity_lua_modules commhm
	func_rsync_miniw_activity_lua_modules ma_gate

	#config
	func_rsync_or_server_config  openresty_ma_gate_x.conf

	echo "cd /usr/local/openresty/nginx/"
	echo "sbin/nginx -c conf/openresty_ma_gate_x.conf"

	func_if_mkdir "/usr/local/openresty/nginx/pids/"
	cd /usr/local/openresty/nginx/
	sbin/nginx -c conf/openresty_ma_gate_x.conf
}


function func_install_dc_gate_server(){
	#rsync code
	func_rsync_miniw_activity_lua_modules commhm
	func_rsync_miniw_activity_lua_modules dc_gate

	#config
	func_rsync_or_server_config  openresty_dc_gate_x.conf

	echo "cd /usr/local/openresty/nginx/"
	echo "sbin/nginx -c conf/openresty_dc_gate_x.conf"

	func_if_mkdir "/usr/local/openresty/nginx/pids/"
	cd /usr/local/openresty/nginx/
	sbin/nginx -c conf/openresty_dc_gate_x.conf
}



#=====================================================
#================= rent 租赁服部分 ===================



# set rsync clinet config file
function func_install_rsync_client_123(){
	if  [ -f "/tmp/rsyncd.secrets_132_client" ]; then
		echo "func_install_rsync_client_123 before ok"
		return 1
	fi

	echo 'miniw#123#' > /tmp/rsync_132.secrets_client_123
    chmod 600 /tmp/rsync_132.secrets_client_123

    echo "install func_install_rsync_client_123 ok"
}


function func_install_rsync_client_907(){
	if  [ -f "/tmp/rsync_132.secrets_r" ]; then
		echo "func_install_rsync_client_907 before ok"
		return 1
	fi

	echo 'miniw_lua_config_ly907_read_132' > /tmp/rsync_132.secrets_client_r_907
	chmod 600 /tmp/rsync_132.secrets_client_r_907

    echo "install func_install_rsync_client_907 ok"
}


function  copy_lua_env0() {

	if [ -d "/data/samba/haima/lua/modules/env/" ]; then
		echo "dir /data/samba/haima/lua/modules/env/ existed."
	else
		mkdir /data/samba/haima/lua/modules/env/ -p
	fi

	cp /home/openresty_lua_modules_env_env000.lua  /data/samba/haima/lua/modules/env/env.lua -f
	chmod 444 /data/samba/haima/lua/modules/env/env.lua

	cat /data/samba/haima/lua/modules/env/env.lua
}



#install start reload sh (docker-in)
function install_start_reload_nginx_sh() {
	func_install_rsync_client_123
	/usr/bin/rsync  --progress --port=8873 -avrcz \
			samba@120.24.64.132::132_haima/openresty_configs/conf0_74_rent_node1/*.sh \
			/usr/local/openresty/nginx/ \
			--password-file /tmp/rsync_132.secrets_client_123
	chmod 700 /usr/local/openresty/nginx/*.sh
}



#docker-in ( confs need reset when node or keeper changed )
function install_rent_node_config0(){

	useradd -s /sbin/nologin -M nginx

	func_install_rsync_client_123

	/usr/bin/rsync  --progress --port=8873 -avrcz \
            samba@120.24.64.132::132_haima/openresty_configs/conf0_74_rent_node1/conf/ \
            /data/samba/haima/conf/ \
            --password-file /tmp/rsync_132.secrets_client_123


	#==========rent_server dirs and configs =========
	if  [ -f "/data/rent_server/HD_config.lua" ]; then
		echo "/data/rent_server/HD_config.lua set before ok"
	else

		func_if_mkdir "/data/rent_server/mini_data/delete_tmp/"
		func_if_mkdir "/data/rent_server/logs/"
		func_if_mkdir "/data/rent_server/pid/"

		chmod 777 /data/rent_server/mini_data/
		chmod 777 /data/rent_server/mini_data/delete_tmp/
		chmod 777 /data/rent_server/logs/
		chmod 777 /data/rent_server/pid/

		/usr/bin/rsync  --progress --port=8873 -avrcz \
				samba@120.24.64.132::132_haima/openresty_configs/conf0_74_rent_node1/HD_config.lua \
				/data/rent_server/ \
				--password-file /tmp/rsync_132.secrets_client_123

		/usr/bin/rsync  --progress --port=8873 -avrcz \
				samba@120.24.64.132::132_haima/openresty_configs/conf0_74_rent_node1/rent_configs/ \
				/data/rent_server/rent_configs/ \
				--password-file /tmp/rsync_132.secrets_client_123

	fi
}



#132_rent_server_rentRsyncRoot_r/home/
function func_rsync_132_node_home() {

	func_install_rsync_client_907

	#/data/rent_server/rentRsyncRoot/home/
	#==========rent_server rentRsyncRoot iwo exe files =========
	/usr/bin/rsync  --progress --port=8873 -avrcz \
				samba@120.24.64.132::132_rent_server_rentRsyncRoot_r/home/ \
				/home/ \
				--password-file /tmp/rsync_132.secrets_client_r_907

	chmod 700 /home/*.sh
}



# 132测试源站机器
# rent server node gs [res] and [exe]
function install_rent_node_gs0() {

	cd /data/rent_server

	func_install_rsync_client_907

	#==========rent_server rentRsyncRoot iwo exe files =========
	/usr/bin/rsync  --progress --port=8873 -avr --exclude "home" --exclude "install_from_132_local*.sh" \
				samba@120.24.64.132::132_rent_server_rentRsyncRoot_r/ \
				/data/rent_server/ \
				--password-file /tmp/rsync_132.secrets_client_r_907

	#rm /data/rent_server/install_from_132_local_env*.sh
	#rm /data/rent_server/install_from_132_local_ver.sh
}



# 可以从华为云分流机器去同步，避免挤占132测试机器的流量（华为云外网）
# 需要先将这台机器 OF1-500 的内容从132同步过去
# rent server node gs [res] and [exe] (华为云) 121.37.244.167_192.168.0.206
function install_rent_node_gs0_hw1() {

	cd /data/rent_server

	func_install_rsync_client_907

	#==========rent_server rentRsyncRoot iwo exe files =========
	/usr/bin/rsync  --progress --port=8873 -avr --exclude "home" --exclude "install_from_132_local*.sh" \
				samba@121.37.244.167::132_rent_server_rentRsyncRoot_r/ \
				/data/rent_server/ \
				--password-file /tmp/rsync_132.secrets_client_r_907

	#rm /data/rent_server/install_from_132_local_env*.sh
	#rm /data/rent_server/install_from_132_local_ver.sh
}


# 可以从华为云分流机器去同步，避免挤占132测试机器的流量（华为云内网）
# 需要先将这台机器 OF1-500 的内容从132同步过去
# rent server node gs [res] and [exe] (华为云) 121.37.244.167_192.168.0.206
function install_rent_node_gs0_hw2() {

	cd /data/rent_server

	func_install_rsync_client_907

	#==========rent_server rentRsyncRoot iwo exe files =========
	/usr/bin/rsync  --progress --port=8873 -avrcz --exclude "home" --exclude "install_from_132_local*.sh" \
				samba@192.168.0.206::132_rent_server_rentRsyncRoot_r/ \
				/data/rent_server/ \
				--password-file /tmp/rsync_132.secrets_client_r_907

	#rm /data/rent_server/install_from_132_local_env*.sh
	#rm /data/rent_server/install_from_132_local_ver.sh
}




#同步git下的 or-lua 代码
function rsyncd_132_lua_code_files() {
	echo  "same as /home/rsync_lua_modules.sh"

	func_install_rsync_client_907
	/usr/bin/rsync  --progress --port=8873 -avrcz --exclude "env" \
				samba@120.24.64.132::132_haima_code_miniw_activity_r/lua/modules/ \
				/data/samba/haima/lua/modules/ \
				--password-file /tmp/rsync_132.secrets_client_r_907

	cat /data/samba/haima/lua/modules/env/env.lua
}



function install_docker(){

	if  [ -f /.dockerenv ]; then
		echo "ERROR, running in docker now ... stoped "
		exit
	fi

	#yum update -y

	#yum remove	docker docker-client docker-client-latest docker-common \
				#docker-latest docker-latest-logrotate \
				#docker-logrotate docker-engine

	#yum install -y yum-utils device-mapper-persistent-data lvm2

	#yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
	#yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

	#open
	#yum-config-manager --enable docker-ce-nightly
	#yum-config-manager --enable docker-ce-test

	#close
	#yum-config-manager --disable docker-ce-nightly
	#yum-config-manager --disable docker-ce-test
	#yum install -y docker-ce docker-ce-cli containerd.io


	#container-selinux 低版本报错
	#wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
	#yum install epel-release   #阿里云上的epel源
	#yum install container-selinux


	yum install -y /data/rent_server/rpm/containerd.io-1.4.3-3.1.el7.x86_64.rpm
	yum install -y /data/rent_server/rpm/docker-ce-cli-19.03.12-3.el7.x86_64.rpm
	yum install -y /data/rent_server/rpm/docker-ce-19.03.12-3.el7.x86_64.rpm

	systemctl start docker

	docker version

}


function run_docker_image(){
	echo "begin run docker rent1"

	#docker run -dit -v /home/:/home/ -v /data/:/data/ --net=host --name='rent1' rent_node:latest /bin/bash
	docker run -dit -v /home/:/home/ -v /data/:/data/ --shm-size="2g" --net=host --name='rent1' rent_node:latest /bin/bash

	echo "try get docker rent1 infos"
	sleep 2
	docker exec rent1 date
	docker exec rent1 tail -n 20 /usr/local/openresty/nginx/start_nginx.log
}




